package com.dragee.modules.work.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.Dic;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.work.entity.ShopEntity;
import com.dragee.modules.work.entity.ShopGoodsEntity;
import com.dragee.modules.work.entity.ShopProjectEntity;
import com.dragee.modules.work.entity.ShopProjectGoodsEntity;
import com.dragee.modules.work.po.UserGoodsPo;
import com.dragee.modules.work.service.ShopProjectGoodsService;
import com.dragee.modules.work.service.ShopProjectService;
import com.dragee.modules.work.service.ShopService;
import com.dragee.utils.LoginUserUtil;
import com.dragee.utils.UploadUtil;
import com.dragee.utils.weiXin.WeiXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 店铺表
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-03-29 14:41:10
 */
@RestController
@RequestMapping("work/shop")
@Api(tags = "work - 店铺表")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private ShopProjectService shopProjectService;
    @Autowired
    private ShopProjectGoodsService shopProjectGoodsService;



    /**
     * 信息
     */
    @GetMapping("getShopByLoginUser")
    @ApiOperation("获取当前登录人的店铺")
    public R getShop(){
        ShopEntity shop =
                shopService.getShopByUserId(LoginUserUtil.getLoginUserId() + "");
        return R.ok().put("data", shop);
    }

    /**
     * 信息
     */
    @GetMapping("getShopByShopId")
    @ApiOperation("根据shopId获取店铺信息")
    public R getShopByShopId(@RequestParam("shopId") String shopId){
        ShopEntity shop = shopService.getById(shopId + "");
        return R.ok().put("data", shop);
    }

    /**
     * 信息
     */
    @GetMapping("getProjects")
    @ApiOperation("获取店铺的所有品项")
    public R getProjects(@RequestParam Map<String, Object> params){
        PageUtils page = shopProjectService.queryPage(params);
        return R.ok().put("data", page);
    }





    /**
     * 信息
     */
    @GetMapping("getGoodsDic")
    @ApiOperation("字典-获取用品")
    public R getGoodsDic(String shopId){
        List<ShopGoodsEntity> goods = shopService.getGoods(shopId);
        List<Dic> result = new ArrayList<>();
        for (ShopGoodsEntity good : goods) {
            result.add(new Dic(good.getName(), good.getId().toString()));
        }
        return R.ok().put("data", result);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改店铺表")
    public R saveOrUpdate(@RequestBody ShopEntity shop){
        shop.setPersonId(LoginUserUtil.getLoginUserId() + "");
        shopService.saveOrUpdate(shop);
        return R.ok();
    }



//===================================================================================
//===================================================================================

    @GetMapping("getProject/{id}")
    @ApiOperation("根据ID获取品相")
    public R getProjectById(@PathVariable("id") String id){
        ShopProjectEntity project = shopService.getProjectById(id);
        return R.ok().put("data", project);
    }


    /**
     * 保存
     */
    @PostMapping("/project/saveOrUpdate")
    @ApiOperation("保存或修改品项")
    public R saveOrUpdate(@RequestBody ShopProjectEntity shopProject){
        shopProjectService.saveOrUpdate(shopProject);
        List<UserGoodsPo> goods = shopProject.getGoods();
        if (goods != null) {
            for (UserGoodsPo good : goods) {
                ShopProjectGoodsEntity pg = new ShopProjectGoodsEntity();
                pg.setShopId(shopProject.getShopId());
                pg.setProjectId(shopProject.getId().toString());
                pg.setId(StrUtil.isBlank(good.getPgId()) ? null : Long.parseLong(good.getPgId()));
                pg.setGoodId(good.getId());
                pg.setSum(good.getSum());
                shopProjectGoodsService.saveOrUpdate(pg);
            }
        }
        return R.ok();
    }

    @Value("${webull.mapUploadPath}")
    private String rootPath;

    /**
     * 生成小程序二维码
     */
    @GetMapping("/createQrCode")
    @ApiOperation("生成店铺的小程序二维码")
    public R createQrCode (){
        ShopEntity shop = shopService.getShopByUserId(LoginUserUtil.getLoginUserId().toString());
        String fileName = shop.getId()+".png";
        String dirPath = rootPath + "//qrCode";
        boolean result = WeiXinUtil.getWXCode(shop.getId().toString(), fileName, dirPath);

        String filePath = dirPath + "//" + fileName;
        shop.setFileName(fileName);
        shop.setFilePath("/work/file" + filePath);
        shopService.saveOrUpdate(shop);
        return R.ok();
    }


    /**
     * 生成小程序二维码
     */
    @PostMapping("/uploadProjectPic")
    @ApiOperation("上传品相图片")
    public R uploadProjectPic (@RequestParam("file") MultipartFile file, @RequestParam("projectId") String projectId){
        ShopProjectEntity shopProjectEntity = shopProjectService.getById(projectId);
        String fileName = file.getOriginalFilename();
        String dirPath = rootPath + "//shopProjectPic//" + shopProjectEntity.getShopId();
        String filePath = dirPath + "//" + fileName;
        UploadUtil.onlyUpload(file, dirPath);
        shopProjectEntity.setFileName(fileName);
        shopProjectEntity.setFilePath("/work/file"+filePath);
        shopProjectService.saveOrUpdate(shopProjectEntity);
        return R.ok();
    }

    /**
     * 生成小程序二维码
     */
    @PostMapping("/uploadMoneyPic")
    @ApiOperation("上传店铺二维码")
    public R uploadMoneyPic (@RequestParam("file") MultipartFile file,
                             @RequestParam("shopId") String shopId){
        ShopEntity shop = shopService.getById(shopId);
        String fileName = file.getOriginalFilename();
        String dirPath = rootPath + "//moneyPic//" + shopId;
        String filePath = dirPath + "//" + fileName;
        UploadUtil.onlyUpload(file, dirPath);
        shop.setFileNameMoney(fileName);
        shop.setFilePathMoney("/work/file"+filePath);
        shopService.saveOrUpdate(shop);
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/project/delete-by-id")
    @ApiOperation("品项删除")
    public R delete( String id){
        shopProjectService.removeById(Long.parseLong(id));
        shopProjectGoodsService.remove(new QueryWrapper<ShopProjectGoodsEntity>().eq("projectId", id));
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/projectAndGood/delete-by-id")
    @ApiOperation("删除品项和库存的关联")
    public R projectAndGoodDelete(String id){
        shopProjectGoodsService.removeById(id);
        return R.ok();
    }


    @PostMapping("/projectAndGood/save")
    @ApiOperation("添加品项和库存的关联")
    public R projectAndGoodSave(@RequestBody ShopProjectGoodsEntity shopProjectGoodsEntity){
        shopProjectGoodsService.saveOrUpdate(shopProjectGoodsEntity);
        return R.ok();
    }





}
